package com.sport.facilities.booking.system;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AnimationUtils;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebView;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.Toast;
import android.widget.ViewFlipper;

import com.sport.facilities.booking.system.httptask.NoticeBoardTask;
import com.sport.facilities.booking.system.httptask.OnTaskCompleted;
import com.sport.facilities.booking.system.ui.ButtonAdapter;
import com.sport.facilities.booking.system.ui.DialogHelper;

/*
 * This Activity is included with MENU_PAGE and NOTICE_BOARD Layout
 * 
 * */
public class MainActivity extends ControlActivity {
	private static final String TAG = "MainActivity";
	private boolean hasNotice = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		Log.d(TAG, "onCreate");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		detector = new GestureDetector(this);

		GridView gridview = (GridView) findViewById(R.id.gridview);
		gridview.setAdapter(new ButtonAdapter(this));

		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		Log.d(TAG, "onCreateOptionsMenu");
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	
	public void next(View view) throws Exception {
		ViewFlipper vf = (ViewFlipper) findViewById(R.id.layout_flipper);
		vf.setInAnimation(AnimationUtils.loadAnimation(
				getApplicationContext(), R.anim.slide_in_right));
		vf.showNext();
	}
	
	
	public boolean onMenuItemSelected(int menuId, MenuItem item) {
		Log.d(TAG, "onMenuItemSelected : " + item.getItemId());
		switch (item.getItemId()) {
		case R.id.menu_settings:
			Toast.makeText(this, "Setting Menu Clicked", Toast.LENGTH_LONG)
					.show();
			return true;
		}
		return false;
	}

	@Override
	public void onResume() {
		super.onResume();
		if (hasNotice == false) {
			fnGetNoticeData();
		}
	}


	
	
	/*
	 * Set the notice onto the user interface
	 */
	public void fnSetNotice(String notice) {
		WebView webView = (WebView) findViewById(R.id.web_notice);
		
		webView.loadData(fnAddCssIntoHtml(notice), "text/html", "UTF-8");
		webView.setInitialScale(230);

		webView.getSettings().setLoadWithOverviewMode(true);
		webView.getSettings().setUseWideViewPort(true);

		webView.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN); 
		webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);  
	}

	/*
	 * Load the notice board data from web by AsyncTask
	 */
	private void fnGetNoticeData() {
		final MainActivity nbActivity = this;
		OnTaskCompleted completeAction = new OnTaskCompleted() {
			public void onTaskCompleted(Object notice) {
				hasNotice = true;
				nbActivity.fnSetNotice(((String) notice));
			}

			public void onTaskFailed(String errMessage) {
				Log.e(TAG, errMessage);
				DialogHelper.getOkAlertDialog(nbActivity, "Error Loading Notice", errMessage).show();
			}
		};
		NoticeBoardTask nbTask = new NoticeBoardTask(this, completeAction);
		nbTask.fnGetNotice();
	}
/*
	public String fnAddCssIntoHtml(String noticeHtml) {
		String notice_board_css = "<style type=\"text/css\">" +
		"body {" +
		"	font-family: verdana,arial,sans-serif;" +
		"	font-size:11px;" +
		"	color:#333333;" +		
		"}" +
		"div b font" +
		"{" +
		"	font-family: verdana,arial,sans-serif;" +
		"	font-size:11px;" +
		"	color:#333333;" +
		"}" +
		"table {" +
		"	margin-top:5px;" +
		"	font-family: verdana,arial,sans-serif;" +
		"	font-size:11px;" +
		"	color:#333333;" +
		"	width:300px;" +
		"	border-width: 1px;" +
		"	border-color: #666666;" +
		"	border-collapse: collapse;" +
		"}" +
		"table th {" +
		"	border-width: 0px;" +
		"	padding: 8px;" +
		"	border-style: solid;" +
		"	border-color: #666666;" +
		"	background-color: #dedede;" +
		"	color:#333333;" +
		"}" +
		"table td {" +
		"	border-width: 0px;" +
		"line-height:10px;" +
		//"width:260px;"+
		"	padding: 8px;" +
		"	border-style: solid;" +
		"	border-color: #666666;" +
		"	background-color: #ffffff;" +
		"	color:#333333;white-space: nowrap;" +
		"}" +
		"font {" +
		"line-height:10px;" +
		"margin-top:2px;" +
		"	font-family: verdana,arial,sans-serif;" +
		"	font-size:11px;" +
		"	color:#333333;" +
		"}" +
		"</style><body>";		
		
		/*
		noticeHtml = noticeHtml.replaceAll("(?i)<table.*?>", "<table>");
		noticeHtml = noticeHtml.replaceAll("(?i)<body.*?>", "<body><table style='position:absolute;margin-top:-190px;width:200px'><tr><td valign=top>");
		noticeHtml = noticeHtml.replaceAll("(?i)<body>", notice_board_css);
		noticeHtml = noticeHtml.replaceAll("(?i)<meta.*?>", "");
		noticeHtml = noticeHtml.replaceAll("(?i)</body.*?>", "</td></tr></table><body>");	
		noticeHtml = noticeHtml.replaceAll("(?i)<font.*?>", "<font>");
		*/
		
		//noticeHtml = noticeHtml.replaceAll("(?i)*?>", " ");
		
		
		//noticeHtml = noticeHtml.replaceAll("(?i)<div.*?>", "");
		//noticeHtml = noticeHtml.replaceAll("(?i)</div>", "");		
		//noticeHtml = noticeHtml.replaceAll("</td><br/>", "</td></tr>");
		//noticeHtml = noticeHtml.replaceAll("<tr><td><b><u><font>Closure of Sports Facilities", "<Table><tr><td><b><u><font>Closure of Sports Facilities");
		//noticeHtml = noticeHtml.replaceAll("</font></font></td></tr><br/>", "</font></font></td></tr></table>");
		

		// This log consume too much memory
		// Log.e(TAG, noticeHtml);

		//return noticeHtml;
	//}
	
    public static String fnAddCssIntoHtml(String noticeHtml)
    {
          // Remove HTML tag from java String    
    	String noHTMLString = noticeHtml.replaceAll("(?i)<br/>", "&-&-&-&");
    	
    	noHTMLString = noHTMLString.replaceAll("(?i)<p>", "&-&-&-&");  
    	noHTMLString = noHTMLString.replaceAll("(?i)</table>", "&-&-&-&");  

    	noHTMLString = noHTMLString.replaceAll("(?i)<B><U>", "&~&~&~&");  
    	noHTMLString = noHTMLString.replaceAll("(?i)<U><B>", "&~&~&~&");  
    	noHTMLString = noHTMLString.replaceAll("(?i)</B></U>", "-=-=-=-");  
    	noHTMLString = noHTMLString.replaceAll("(?i)</U></B>", "-=-=-=-");  
    	
    	
    	noHTMLString = noHTMLString.replaceAll("\\<.*?\\>", "");

        // Remove Carriage return from java String

        // Remove New line from java string and replace html break
    	

        noHTMLString = noHTMLString.replaceAll("&~&~&~&", "<br/><B><U><font color=orange>");
        noHTMLString = noHTMLString.replaceAll("-=-=-=-", "</font></B></U>");

        noHTMLString = noHTMLString.replaceAll("&-&-&-&", "<br/>");
        noHTMLString = noHTMLString.replaceAll("Sports Facilities Booking System", "");
        noHTMLString = noHTMLString.replaceAll("window.open(url,winname , features);", "");
        noHTMLString = noHTMLString.replaceAll("Welcome to Sports Facilities Booking System", "");
        noHTMLString = noHTMLString.replaceAll("Notice Board", "");
        

        noHTMLString = noHTMLString.replaceAll("\n", " ");
        noHTMLString = noHTMLString.replaceAll("\'", "&#39;");
        noHTMLString = noHTMLString.replaceAll("\"", "&quot;");
        noHTMLString = noHTMLString.replaceAll("\r", "<br/>");
        	
        String first ="<table style='position:absolute;width:280px;font-family: verdana,arial,sans-serif;font-size:10px;margin-top:-60px'><tr><td valign=top>";
        String last ="</td></tr></table></body></html>";
        Log.e(TAG, first + noHTMLString + last);
        return first + noHTMLString + last;
    }
}
